package com.zk.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;

/**
 * @author ZK
 * @Description
 * @time 2021/5/11 12:28
 * @note
 */
public class MybatisUtils {
    private static SqlSessionFactory ssf = null;
    static {
        String configFile = "mybatis.xml";
        try {
            InputStream is = Resources.getResourceAsStream(configFile);
            ssf = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession() {
        SqlSession sqlSession = null;
        if (ssf != null) {
            sqlSession = ssf.openSession();
        }
        return sqlSession;
    }
    public static SqlSession getSqlSession(boolean autoCommit) {
        SqlSession sqlSession = null;
        if (ssf != null) {
            sqlSession = ssf.openSession(autoCommit);
        }
        return sqlSession;
    }
}
